<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\service\OrderPayment as OrderPaymentService;
use app\admin\validate\OrderPayment as OrderPaymentValidate;

class OrderPayment
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['order_no'] =  $request->param('order_no', null);
        $param['order_ids'] =  $request->param('order_ids', null);
        $param['voucher_code'] =  $request->param('voucher_code', null);
        $param['order_type'] =  $request->param('order_type', null);
        $param['pay_type'] =  $request->param('pay_type', null);
        $param['pay_status'] =  $request->param('pay_status', null);
        $param['start_time'] =  $request->param('start_time', null);
        $param['end_time'] =  $request->param('end_time', null);
        $param['invoice_status'] =  $request->param('invoice_status', null);
        $param['user_id'] =  $request->param('user_id', null);
        $param['creator'] =  $request->param('creator', null);
        $param['invoive_id'] =  $request->param('invoive_id', null);

        $intPage =  $request->param('pageIndex', 1);
        $intPageSize =  $request->param('pageSize', 50);
        $arrData = $service->index($param, (int)$intPage, (int)$intPageSize);
        return json($arrData);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function resource(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {
        $param['field'] =  $request->param('field', 'ALL');
        $arrData = $service->resource($param['field']);
        return json($arrData);
    }

    /**
     * @describe 审核支付
     * @return Renderable
     */
    public function examine(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {
        $checkData = $request->param();


        if (!$validate->scene('examine')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);
        $param['paid_amount'] = $request->param('paid_amount', null);
        $param['voucher_code'] = $request->param('voucher_code', null);
        $param['fee_type'] = $request->param('fee_type', null);
        // $param['pay_status'] = $request->param('pay_status', null);
        $param['is_approved'] = $request->param('is_approved', null);
        $param['remark'] = $request->param('remark', null);
        $param['transfer_recepit_id'] = $request->param('transfer_recepit_id', []);
        $param['payment_method'] = $request->param('payment_method', null);

        $arrData = $service->examine($param);
        return json($arrData);
    }
    /**
     * @describe 审核支付
     * @return Renderable
     */
    public function cancellVoucher(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {
        $checkData = $request->param();


        if (!$validate->scene('cancel')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);


        $arrData = $service->cancellVoucher($param);
        return json($arrData);
    }


    /**
     *  上传支付凭证
     */
    public function voucher(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {


        $param['creator'] = $request->operator_id;
        $param['order_id'] = $request->param('order_id', null);
        $param['order_type'] = $request->param('order_type', null);
        $param['voucher_id'] = $request->param('voucher_id', null);
        $param['notes'] = $request->param('notes', null);
        $param['operator_id'] = $request->operator_id;
        $param['voucher_code'] = $request->param('voucher_code', null);
        $param['paid_amount'] = $request->param('paid_amount', null);
        $param['fee_type'] = $request->param('fee_type', null);
        $param['transfer_type'] = $request->param('transfer_type', null);
        $param['payment_method'] = $request->param('payment_method', null);
        if (!$validate->scene('voucher')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $arrData = $service->voucher($param);
        return json($arrData);
    }

    /**
     * @describe 获取订单相关支付列表
     * @author: Bobo
     * @date: 2025-08-06 14:40:15 
     * @param [type] @required
     * @return Renderable
     */
    public function orderRelatedPaymentList(Request $request, OrderPaymentService $service, OrderPaymentValidate $validate)
    {
        $param['order_id'] = $request->param('order_id', null);
        if (!$validate->scene('order_related_payment_list')->check($param)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $arrData = $service->orderRelatedPaymentList($param);
        return json($arrData);
    }

    /**
     * 导出支付订单数据到Excel
     * @param Request $request
     * @param OrderPaymentService $service
     * @return void
     */
    public function export(Request $request, OrderPaymentService $service)
    {
        // 获取查询参数
        $param['order_no'] = $request->param('order_no', null);
        $param['voucher_code'] = $request->param('voucher_code', null);
        $param['order_type'] = $request->param('order_type', null);
        $param['pay_type'] = $request->param('pay_type', null);
        $param['pay_status'] = $request->param('pay_status', null);
        $param['start_time'] = $request->param('start_time', null);
        $param['end_time'] = $request->param('end_time', null);
        $param['invoice_status'] = $request->param('invoice_status', null);
        $param['user_id'] = $request->param('user_id', null);
        $param['creator'] = $request->param('creator', null);

        // 调用服务层的导出方法
        $service->exportOrderPayment($param);
    }
}
